//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.services
{
	import flash.utils.Dictionary;
	
	import mx.core.UIComponent;
	
	import reader.model.IDraftProxy;
	import reader.model.IServiceProxy;
	import reader.model.vo.ServiceVO;
	
	public class ServiceFactory
	{
		/**
		 * create an IPostBlogAble instance or fetch ir ftom given service pool.
		 * @param service stored the service information.
		 * @param serviceProxy proxy used for store and get ServiceVO.
		 * @param draftProxy proxy used for store and get DraftVO.
		 * @param parent used for display information, such as alert.
		 * @param servicePool if servicePool is not null, all created serivice will stored in it and reused.
		 */
		public static function getPostBlogService(service:ServiceVO, 
													serviceProxy:IServiceProxy, 
													draftProxy:IDraftProxy, 
													parent:UIComponent = null, 
													servicePool:Dictionary = null):IPostBlogAble
		{
			if(service.type == ServiceVO.SERVICE_OTHER)
			{
				switch(service.name)
				{
					case ServiceVO.OTHER_MYSPACE_CN:
						var myspaceCn:MyspaceCnReaderService;
						if(!servicePool[service.sid])
						{
							myspaceCn = new MyspaceCnReaderService(service, serviceProxy, parent);
							myspaceCn.iDraftProxy = draftProxy;
							myspaceCn.isPostBlogAble = true;
						}
						else
						{
							myspaceCn = servicePool[service.sid];
						}
						return myspaceCn;
				}
			}
			else if(service.type == ServiceVO.SERVICE_METAWEBLOG)
			{
				var metablog:MetaWeblogBaseService;
				if(!servicePool[service.sid])
				{
					metablog = new MetaWeblogBaseService(service, serviceProxy, parent);
					metablog.iDraftProxy = draftProxy;
					metablog.isPostBlogAble = true;
					metablog.isUpdateBlogAble = true;
				}
				else
				{
					metablog = servicePool[service.sid];
				}
				return metablog;
			}
			else if(service.type == ServiceVO.SERVICE_ATOM)
			{
				
			}
			
			return null;
		}
		
		/**
		 * create an IGetAlbumAble instance or fetch ir ftom given service pool.
		 * @param service stored the service information.
		 * @param proxy proxy used for store and get ServiceVO.
		 * @param parent used for display information, such as alert.
		 * @param servicePool if servicePool is not null, all created serivice will stored in it and reused.
		 */		
		public static function getGetAlbumService(service:ServiceVO, 
													proxy:IServiceProxy, 
													parent:UIComponent = null, 
													servicePool:Dictionary = null):IGetAlbumAble
		{
			if(service.type == ServiceVO.SERVICE_OTHER)
			{
				switch(service.name)
				{
					case ServiceVO.OTHER_MYSPACE_CN:
						var myspaceCn:MyspaceCnReaderService;
						if(!servicePool[service.sid])
						{
							myspaceCn = new MyspaceCnReaderService(service, proxy, parent);
							myspaceCn.isGetAlbumAble = true;
						}
						else
						{
							myspaceCn = servicePool[service.sid];
						}
						return myspaceCn;
					case ServiceVO.OTHER_FACEBOOK:
						var facebook:FacebookReaderService;
						if(!servicePool[service.sid])
						{
							facebook = new FacebookReaderService(service, proxy, parent);
							facebook.isGetAlbumAble = true;
						}
						else
						{
							facebook = servicePool[service.sid];
						}
						return facebook;
					case ServiceVO.OTHER_FLICKR:
						var flickr:FlickrReaderService;
						if(!servicePool[service.sid])
						{
							flickr = new FlickrReaderService(service, proxy, parent);
							flickr.isGetAlbumAble = true;
						}
						else
						{
							flickr = servicePool[service.sid];
						}
						return flickr;	
				}
			}
			
			return null;
		}
		
		/**
		 * create an IUploadPhotoAble instance or fetch ir ftom given service pool.
		 * @param service stored the service information.
		 * @param serviceProxy proxy used for store and get ServiceVO.
		 * @param draftProxy proxy used for store and get DraftVO.
		 * @param parent used for display information, such as alert.
		 * @param servicePool if servicePool is not null, all created serivice will stored in it and reused.
		 */
		public static function getPostAlbumService(service:ServiceVO, 
													serviceProxy:IServiceProxy, 
													draftProxy:IDraftProxy, 
													parent:UIComponent = null, 
													servicePool:Dictionary = null):IUploadPhotoAble
		{
			if(service.type == ServiceVO.SERVICE_OTHER)
			{
				switch(service.name)
				{
					case ServiceVO.OTHER_MYSPACE_CN:
						var myspaceCn:MyspaceCnReaderService;
						if(!servicePool[service.sid])
						{
							myspaceCn = new MyspaceCnReaderService(service, serviceProxy, parent);
							myspaceCn.isGetAlbumAble = true;
						}
						else
						{
							myspaceCn = servicePool[service.sid];
						}
						if(!myspaceCn.iDraftProxy)
							myspaceCn.iDraftProxy = draftProxy;
						return myspaceCn;
					case ServiceVO.OTHER_FACEBOOK:
						var facebook:FacebookReaderService;
						if(!servicePool[service.sid])
						{
							facebook = new FacebookReaderService(service, serviceProxy, parent);
							facebook.isGetAlbumAble = true;
						}
						else
						{
							facebook = servicePool[service.sid];
						}
						if(!facebook.iDraftProxy)
							facebook.iDraftProxy = draftProxy;
						return facebook;
					case ServiceVO.OTHER_FLICKR:
						var flickr:FlickrReaderService;
						if(!servicePool[service.sid])
						{
							flickr = new FlickrReaderService(service, serviceProxy, parent);
							flickr.isGetAlbumAble = true;
						}
						else
						{
							flickr = servicePool[service.sid];
						}
						if(!flickr.iDraftProxy)
							flickr.iDraftProxy = draftProxy;
						return flickr;	
				}
			}
			
			return null;
		}
		
		/**
		 * create an ISubscribeAble instance or fetch ir ftom given service pool.
		 * @param service stored the service information.
		 * @param serviceProxy proxy used for store and get ServiceVO.
		 * @param parent used for display information, such as alert.
		 * @param servicePool if servicePool is not null, all created serivice will stored in it and reused.
		 */
		public static function getSubscribeService(service:ServiceVO, 
													serviceProxy:IServiceProxy,
													parent:UIComponent = null, 
													servicePool:Dictionary = null):ISubscribeAble
		{
			if(service.type == ServiceVO.SERVICE_OTHER)
			{
				switch(service.name)
				{
					case ServiceVO.OTHER_MYSPACE_CN:
						var myspaceCn:MyspaceCnReaderService;
						if(!servicePool[service.sid])
						{
							myspaceCn = new MyspaceCnReaderService(service, serviceProxy, parent);
							myspaceCn.isSubscribeAble = true;
						}
						else
						{
							myspaceCn = servicePool[service.sid];
						}
						return myspaceCn;
					case ServiceVO.OTHER_FLICKR:
						var flickr:FlickrReaderService;
						if(!servicePool[service.sid])
						{
							flickr = new FlickrReaderService(service, serviceProxy, parent);
							flickr.isSubscribeAble = true;
						}
						else
						{
							flickr = servicePool[service.sid];
						}
						return flickr;		
				}
			}
			
			return null;
		}

	}
}